#include "StdAfx.h"
#include "DeviceBase.h"
#include "RobotInstance.h"


namespace RobotPlant { namespace RobotProxy{

	DeviceBase::DeviceBase(RobotInstance* pParent,joint::spawn_type e):joint(e)
	{
		_pParent = pParent;
		_deviceType = DeviceTypeEnum::Unknow;
		_deviceIndex = 1;

		chord(HardwareID, &DeviceBase::Handler_GetHardwareID);
		chord(Name, &DeviceBase::Handler_GetName);
		chord(Pose, &DeviceBase::Handler_GetPose);

	}

	DeviceBase::~DeviceBase()
	{
	}

	DeviceTypeEnum DeviceBase::GetDeviceType()
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);
		DeviceTypeEnum devType = _deviceType;

		return devType;
	}

	int DeviceBase::GetDeviceIndex()
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);
		int devIndex = _deviceIndex;

		return devIndex;
	}

	int DeviceBase::Handler_GetHardwareID(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		int value = 0;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Device_GetHardwareID(this->_deviceType,this->_deviceIndex,&value) != SOAP_OK)
		{
			value = -1;
		}

		return value;
	}

	std::string DeviceBase::Handler_GetName(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		char* pTemp = NULL;
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		if (robDeviceInterface.ns1__Device_GetName(this->_deviceType,this->_deviceIndex,&pTemp) != SOAP_OK)
		{
			pTemp = NULL;
		}

		std::string name(pTemp);
		return name;
	}

	std::vector<double> DeviceBase::Handler_GetPose(void_t)
	{
		boost::mutex::scoped_lock lock(this->_pParent->_mutex);

		std::vector<double> pose = std::vector<double>();
		RobDeviceInterface robDeviceInterface;
		string addr = _pParent->_soapServer;
		robDeviceInterface.endpoint = addr.data();
		struct ns1__Device_GetPoseResponse temp;
		if (robDeviceInterface.ns1__Device_GetPose(this->_deviceType,this->_deviceIndex,&temp) != SOAP_OK)
		{
			pose.clear();
		}
		else
		{
			for(int i=0; i<7; i++)
				pose.push_back(temp.Data[i]);
		}

		return pose;
	}
}}